//  Copyright 2008 Stefan Kristiansson
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the Lesser GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  Lesser GNU General Public License for more details.
//
//  You should have received a copy of the Lesser GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Ceme;

namespace Calculator
{
    public partial class Calc : Form
    {
        private CemeParser parser;
        public Calc()
        {
            InitializeComponent();
            parser = new CemeParser();
            //Add a custom function that calculates the area of a circle from the radius
            parser.AddFunction("circle", 1, funcCircle);
            //Add a custom function that overrides the builtin abs function
            parser.AddFunction("abs", 1, funcAbs);
            //Add a custom without any arguments
            parser.AddFunction("pi", 0, funcPI);
        }
        
        #region custom functions
        private double funcCircle(double[] args)
        {
            return Math.PI * Math.Pow(args[0],2);
        }
        private double funcAbs(double[] args)
        {
            if (args[0] < 0)
                return -args[0];
            else
                return args[0];
        }
        private double funcPI(double[] args)
        {
            return Math.PI;
        }
        #endregion

        private void calcButton_Click(object sender, EventArgs e)
        {
            calculate();
        }
        private void calculate()
        {
            try
            {
                parser.SetExpression(inputBox.Text);
                string[] varList = parser.GetVariables();
                int i = 0;
                foreach (string var in varList)
                {
                    parser.SetVariable(var, ++i);
                }
                outputBox.Text += inputBox.Text + "\r\n" + string.Format("    = {0}\r\n", parser.Evaluate());
            }
            catch
            {
                outputBox.Text += inputBox.Text + "\r\n" + "    Error: Invalid input\r\n";
            }
            outputBox.SelectionStart = outputBox.Text.Length;
            outputBox.SelectionLength = 0;
            outputBox.ScrollToCaret();
        }

        private void inputBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return)
                calculate();
        }
    }
}